package abg.core.common.util.valid.handler;


import abg.core.common.util.StringKit;
import abg.core.common.util.valid.Length;
import abg.core.common.util.valid.base.ValidBaseHandler;
import abg.core.domain.sys.ServiceException;
import org.springframework.stereotype.Component;

/**
 * 字符串长度校验
 */
@Component
public class LengthHandler implements ValidBaseHandler<Length, String> {

    // 第一个参数应该是注解
    @Override
    public void valid(Length annotation, String paramName, String paramValue) {
        if (StringKit.isBlank(paramValue))
            throw new ServiceException(StringKit.blankDefStr(annotation.msg(), "参数：" + paramName + "，不能为空"));

        long mast = annotation.mast();
        long min = annotation.min();
        long max = annotation.max();

        if (mast != 0) {
            if (paramValue.length() != mast)
                throw new ServiceException(StringKit.blankDefStr(annotation.msg(), "参数：" + paramName + "，长度不等于" + mast));
        }

        if (min != 0) {
            if (paramValue.length() < min)
                throw new ServiceException(StringKit.blankDefStr(annotation.msg(), "参数：" + paramName + "，长度小于" + min));
        }

        if (max != 0) {
            if (paramValue.length() > max)
                throw new ServiceException(StringKit.blankDefStr(annotation.msg(), "参数：" + paramName + "，长度大于" + max));
        }


    }

}
